package love.yuanmeng.guetcoalsystem.service.impl;/*
 *   @Studio:CHJY · 源梦科技
 *   @Author:zjj
 *   @Ide:IntelliJ IDEA
 *   @Creation time:2021/10/10 16:46
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import love.yuanmeng.guetcoalsystem.pojo.Message;
import love.yuanmeng.guetcoalsystem.service.MessageService;
import love.yuanmeng.guetcoalsystem.vo.NewsAndPage;
import love.yuanmeng.guetcoalsystem.mapper.NewsMapper;
import love.yuanmeng.guetcoalsystem.pojo.News;
import love.yuanmeng.guetcoalsystem.service.NewsService;
import love.yuanmeng.guetcoalsystem.util.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class NewsServiceImpl implements NewsService {
    @Autowired
    private NewsMapper newsMapper;
    @Autowired
    private MessageService messageService;

    @Override
    public boolean addNew(News news, MultipartFile file) {
        int update=0;
        try {
            String savePath = ResourceUtils.getURL("classpath:").getPath()+"static/img/newImg/";
            File savePathFile = new File(savePath);
            if(!savePathFile.exists()){
                savePathFile.mkdir();
            }
            String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1, file.getOriginalFilename().length());
            String filename = UUID.randomUUID().toString().replaceAll("-","") + "." + suffix;
            System.out.println(savePath + filename);
            file.transferTo(new File(savePath+filename));
            news.setNewImg("/img/newImg/"+filename);
            news.setNewStatus("待审核");
            news.setNewTime(RandomUtils.getTime());
            return newsMapper.insert(news)==1;
        }catch (Exception e){
            e.printStackTrace();
        }
        return update>0;
    }

    @Override
    public boolean editNew(News news, MultipartFile file) {
        QueryWrapper<News> queryWrapper = new QueryWrapper<News>();
        int update=0;
        try {
            String savePath = ResourceUtils.getURL("classpath:").getPath()+"static/img/newImg/";
            File savePathFile = new File(savePath);
            if(!savePathFile.exists()){
                savePathFile.mkdir();
            }
            String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1, file.getOriginalFilename().length());
            String filename = UUID.randomUUID().toString().replaceAll("-","") + "." + suffix;
            System.out.println(savePath + filename);
            file.transferTo(new File(savePath+filename));
            news.setNewImg("/img/newImg/"+filename);
            news.setNewStatus("待审核");
            news.setNewTime(RandomUtils.getTime());
            queryWrapper.eq("id",news.getId());
            return newsMapper.update(news,queryWrapper)==1;
        }catch (Exception e){
            e.printStackTrace();
        }
        return update>0;
    }

    @Override
    public IPage getNews(NewsAndPage newsAndPage) {
        Date endTime = newsAndPage.getEndTime();
        Date startTime = newsAndPage.getStartTime();
        News news = newsAndPage.getNews();
        Page page = newsAndPage.getPage();


        QueryWrapper<News> newsExcludeEmptyQueryWrapper = new QueryWrapper<News>();
        newsExcludeEmptyQueryWrapper.eq(!"全部".equals(news.getNewStatus()),"new_status", news.getNewStatus())
                .eq(!StringUtils.isBlank(news.getUserid()),"userid",news.getUserid())
                .gt("new_time", startTime)
                .lt("new_time", endTime)
                .like(!StringUtils.isBlank(news.getNewTitle()),"new_title",news.getNewTitle())
                .orderByDesc("new_time");


        return newsMapper.selectPage(page,newsExcludeEmptyQueryWrapper);
    }

    @Override
    public List<News> getRecent(News news) {
        Page<News> newsPage = new Page<>();
        newsPage.setSize(6);
        newsPage.setCurrent(1);
        QueryWrapper<News> queryWrapper = new QueryWrapper<News>();
        queryWrapper.orderByAsc("new_time")
                .eq(news.getUserid()!=null,"userid",news.getUserid())
                .eq(!StringUtils.isBlank(news.getNewStatus()),"new_status",news.getNewStatus());
        newsMapper.selectPage(newsPage,queryWrapper);
        return newsPage.getRecords();
    }

    @Override
    public boolean editNewNullImg(News news) {
        UpdateWrapper<News> updateWrapper = new UpdateWrapper<News>();
        updateWrapper.eq("id",news.getId());

        return newsMapper.update(news,updateWrapper)==1;
    }

    @Override
    public int deleNew(News news) {
        LambdaQueryWrapper<News> wrapper = new QueryWrapper<News>().lambda();
        wrapper.eq(News::getId,news.getId());
        return newsMapper.delete(wrapper);
    }

    @Override
    public boolean auditNews(News news,Message message) {
        return editNewNullImg(news)&&messageService.inserMessage(message);
    }
}
